package map;

public class BSTMap<K extends Comparable<K>,V> implements Map<K,V> {


    private class Node{
        public K key;
        public V value;
        public Node left,right;

        public Node(K key,V value){
            this.key = key;
            this.value = value;
            left = null;
            right = null;
        }

    }

    private Node root;
    private int size;

    public BSTMap(){
    }

    /**
     * 二分搜索树添加新元素
     * @param key
     * @param value
     */
    @Override
    public void add(K key, V value) {
        root = add(root,key,value);
    }

    //向以node为根的二分搜索树中插入元素e，递归算法
    // 返回插入新节点后二分搜索树的根
    private Node add(Node node,K key,V value){
        if(node == null){
            size++;
            return new Node(key,value);
        }

        if(key.compareTo(node.key) < 0){
            //插入到左子树
            node.left = add(node.left,key,value);
        }else if(key.compareTo(node.key) > 0){
            //插入到右子树
            node.right = add(node.right,key,value);
        }else {
            node.value = value;
        }

        return node;

    }


    // 返回以node为根节点的二分搜索树中，key所在的节点
    private Node getNode(Node node,K key){
        if(node == null){
            //node为null，表示没有相等的节点
            return null;
        }

        if(key.compareTo(node.key) == 0){
            //找到了与key相等的节点
            return node;
        }else if(key.compareTo(node.key) < 0){
            //在左子树上找
            return getNode(node.left,key);
        }else {
            //在右子树上找
            return getNode(node.right,key);
        }


    }

    @Override
    public V remove(K key) {

        return null;
    }



    @Override
    public boolean contains(K key) {

        return getNode(root,key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root,key);
        if(node != null){
            return node.value;
        }
        return null;
    }

    @Override
    public void set(K key, V value) {
        Node node = getNode(root,key);
        if(node == null){
            throw new IllegalArgumentException("key 不存在");
        }
        node.value = value;
    }

    @Override
    public int getSize() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    // 删除掉以node为根的二分搜索树中键为key的节点，递归算法
    // 返回删除节点后新二分搜索树的根
    private Node remove(Node node,K key){
        if(node == null){
            return null;
        }

        if(key.compareTo(node.key) < 0){
            //遍历左子树
            node.left = remove(node.left,key);
            return node;
        }else if(key.compareTo(node.key) > 0){
            //遍历右子树
            node.right = remove(node.right,key);
            return node;
        }else {

            //待删除节点的左子树为空的情况
            if(node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }

            //待删除节点的右子树为空的情况
            if(node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }


        }

        return null;
    }
}
